/*
 *      Sub.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.arimethic;

import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.Operations;
import ilanguage.iltokens.operations.TypeSafeTwoOperandIL_SSA;
import lexer.EntryType;
/**
 * The Class Sub.
 */
public class Sub extends TypeSafeTwoOperandIL_SSA {

	/**
	 * Instantiates a new sub.
	 *
	 * @param firstToken the first token
	 * @param secToken the sec token
	 */
	public Sub(IL_SSA firstToken, IL_SSA secToken) {
		super(firstToken, secToken);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.SequenciableToken#GenerateCodeMachine()
	 */
	@Override
	public String GenerateCode() {
		 String firstArg = this.getFirstArg().getResultRegister();
		 String secondArg = this.getSecondArg().getResultRegister();
		 String codigo="";
		 codigo+="\t; Comienzo de Resta\n";

	     if (this.solveTypes() == EntryType.INT)
	    	 codigo+=subINTGenerateCode(firstArg,secondArg);
	     else
	    	 codigo+=subDOUBLEGenerateCode(firstArg,secondArg);
	     codigo+="\t; Fin de Resta\n\n";
	     return codigo;
	}

private String subINTGenerateCode(String firstArg, String secondArg) {

		String codigo="";
	if (RegisterAssigner.getInstance().esRegistro(firstArg)){
   		 codigo+="\t\tsub "+firstArg+ " , "+ secondArg+"\n";
   		 RegisterAssigner.getInstance().liberarRegistro(secondArg);
   		 this.setResultRegister(firstArg);
		}
   	 else { //si ninguno de los dos es registro
   		 	String regAux = RegisterAssigner.getInstance().getRegistro();
   		 	codigo+="\t\tmov "+regAux+ " , "+ firstArg+"\n";
   		 	codigo+="\t\tsub "+regAux+ " , "+ secondArg+"\n";
   		 	this.setResultRegister(regAux);

   		 	RegisterAssigner.getInstance().liberarRegistro(firstArg);
   		    RegisterAssigner.getInstance().liberarRegistro(secondArg);
   	 	  }
/*	codigo+=verificarOverFlowINT();*/
	codigo+=verificarUnderFlowINT();
	return codigo;
	}

private String subDOUBLEGenerateCode(String first, String second) {
	String codigo="";

	if ((first !="FPU") && (second !="FPU")){
			codigo+="\t\tfld "+ first+"\n";
			codigo+="\t\tfld "+ second+"\n";
	}
	else if ((first =="FPU") && (second !="FPU")){
			codigo+="\t\tfld "+ second+"\n";
	}
	else if ((first !="FPU") && (second =="FPU")){
			codigo+="\t\tfld "+ first+"\n";
			codigo+="\t\tfxch\n";
	}

	codigo+="\t\tfsub\n";
	this.setResultRegister("FPU");
	codigo+=verificarUnderFlowDOUBLE();
	/*codigo+=verificarOverFlowDOUBLE();*/
	return codigo;
}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.operations.OperationToken#getOperationName()
	 */
	@Override
	public String getOperationName() {
		return Operations.SUB.toString();
	}

}
